<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style-type: none;
        }
        
        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            font-weight: normal;
        }
        
        .top {
            width: 1000px;
            margin: 50px auto;
        }
        
        .top h1 {
            text-align: center;
            margin-top: 50px;
            margin-bottom: 50px;
            font-size: 26px;
            color: darkcyan;
            font-family: "楷体";
        }
        
        .top input {
            margin-top: 50px;
        }
        
        .Wlist li {
            width: 122px;
            height: 246px;
            color: #fff;
            border: 1px solid #1c7ab0;
            background: -webkit-gradient(linear, 0% 0, 0% 100%, from(#50b2fb), to(#a6d4f6));
            float: left;
            position: relative;
            cursor: pointer;
            padding-bottom: 6px;
            text-align: center;
            margin-right: 10px;
        }
        
        .Wlist h2,
        h3,
        h4,
        h5,
        h6 {
            font-size: 14px;
            margin-top: 30px;
        }
        
        .list {
            display: none;
        }
    </style>
</head>

<body>
    <div class="top">
        <input type="text" placeholder="请输入查询城市" class="city">
        <button class="button">查询</button>
        <img src="loading.gif" id="loading" style="display:none;" alt="">
        <h1 class="title" style="display: none;"><span></span>未来7天的天气情况</h1>
        <ul class="Wlist"></ul>
        <li id="list">
            <h2 class="date"></h2>
            <h3 class="weather"></h3>
            <h4 class="tem"></h4>
            <h5 class="win">
                <span class="left"></span>
                <span class="right"></span>
            </h5>
            <h6 class="speed"></h6>

        </li>
    </div>
</body>

</html>
<script>
    function get(data) {
        document.querySelectorAll(".Wlist")[0].innerHTML = "";
        document.getElementById("loading").style.display = "none";
        document.querySelector(".title").style.display = "block";
        document.querySelector("span").innerHTML = document.querySelector(".city").value;
        console.log(data)
            // 编写后端请求
        let D = data.data;

        D.forEach(function(item, index) {
            let list = document.querySelector("#list");
            let Wlist = document.querySelectorAll(".Wlist")[0];
            let newlist = list.cloneNode(true);
            Wlist.appendChild(newlist);
            newlist.id = "";
            newlist.querySelector(".date").innerHTML = item.day;
            newlist.querySelector(".weather").innerHTML = item.wea;
            newlist.querySelector(".tem").innerHTML = item.tem1 + "/" + item.tem2 + "℃";
            if (index === 0) {
                let start = newlist.querySelector(".date").innerHTML;
                let after = start.replace(/星期.{1}/, "今天");
                newlist.querySelector(".date").innerHTML = after;
            }
            newlist.querySelector(".win").innerHTML = item.win;
            newlist.querySelector(".speed").innerHTML = item.win_speed;

        })
    }
    let button = document.querySelector(".button");
    // let city = document.querySelector(".city")
    button.onclick = function() {
        let city = document.querySelector(".city").value;
        let body = document.querySelector("body");
        let script = document.createElement("script");
        document.getElementById("loading").style.display = "block";
        let Wlist = document.querySelectorAll(".Wlist")[0];
        Wlist.style.opacity = 0;
        let time = setInterval(function() {
            let o = parseFloat(Wlist.style.opacity);
            if (o >= 1) {
                Wlist.style.opacit = 0
                clearInterval(time);
                return;
            }
            script.setAttribute("src",
                `https://v0.yiketianqi.com/api?version=v9&appid=46764186&appsecret=45GONP3t&callback=get&city=${city}`);
            body.appendChild(script)
            Wlist.style.opacity = o + 0.005;
        }, 16)
    }
    document.body.onkeydown = function(evt) {
        let e = evt || event;
        if (evt.keyCode === 13) {
            document.querySelector(".button").onclick();
        }
    }
</script>